package perucuy.dao.sqlmap;

import java.sql.*;
import java.util.*;

import com.ibatis.sqlmap.client.*;
import org.apache.log4j.*;

public abstract class SqlMapDAO implements java.io.Serializable{
    private static Logger log = Logger.getLogger(SqlMapDAO.class);
    private SqlMapClient sqlMap;
    protected SqlMapDAO(SqlMapClient sqlMap) {
        this.sqlMap = sqlMap;
    }

    @SuppressWarnings("unchecked")
	protected <T> T queryForObject(String string, Object filtro) {
        try {
            sqlMap.startTransaction();
            T ret = (T) sqlMap.queryForObject(string, filtro);
            sqlMap.commitTransaction();
            return ret;
        } catch (SQLException ex) {
            log.error(ex, ex);
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException ex1) {
                log.warn(ex1, ex1);
            }
        }
        return null;
    }

    protected List queryForList(String string, Object filtro, int skip,
                                int max) {
        try {
            sqlMap.startTransaction();
            List ret = sqlMap.queryForList(string, filtro, skip, max);
            sqlMap.commitTransaction();
            return ret;
        } catch (SQLException ex) {
            log.error(ex, ex);
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException ex1) {
                log.warn(ex1);
            }
        }
        return null;
    }

    @SuppressWarnings("unchecked")
	protected <T> List<T> queryForList(String string, Object object) {
        try {
            sqlMap.startTransaction();
            List<T> ret = sqlMap.queryForList(string, object);
			sqlMap.commitTransaction();
            return ret;
        } catch (SQLException ex) {
            log.error(ex, ex);

        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException ex1) {
                log.warn(ex1, ex1);
            }
        }
        return null;
    }

    @SuppressWarnings("unchecked")
	protected <T> T insert(String string, Object item) {
        try {
            sqlMap.startTransaction();
            T ret = (T) sqlMap.insert(string, item);
            sqlMap.commitTransaction();
            return ret;
        } catch (SQLException ex) {
            log.error(ex, ex);
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException ex1) {
                log.warn(ex1, ex1);
            }
        }
        return null;
    }

    protected Map queryForMap(String string, Object filtro, String string1) {

        Map map = null;
        try {
            sqlMap.startTransaction();
            map = sqlMap.queryForMap(string, filtro, string1);
            sqlMap.commitTransaction();
            return map;
        } catch (SQLException ex) {
            log.error(ex, ex);
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException ex1) {
                log.warn(ex1, ex1);
            }
        }
        return map;
    }

    protected int update(String string, Object param) {
        try {
            sqlMap.startTransaction();
            int ret = sqlMap.update(string, param);
            sqlMap.commitTransaction();
            return ret;
        } catch (SQLException ex) {
            log.error(ex,ex);
        }finally{
            try {
                sqlMap.endTransaction();
            } catch (SQLException ex1) {
                log.warn(ex1,ex1);
            }
        }
        return 0;
    }

    protected int delete(String string, Object param) {
        try {
            sqlMap.startTransaction();
            int ret = sqlMap.delete(string, param);
            sqlMap.commitTransaction();
            return ret;
        } catch (SQLException ex) {
            log.error(ex,ex);
        }finally{
            try {
                sqlMap.endTransaction();
            } catch (SQLException ex1) {
                log.warn(ex1,ex1);
            }
        }
        return 0;
    }
    
    protected List queryForList(String string) {
        try {
           sqlMap.startTransaction();
           List ret = sqlMap.queryForList(string);
           sqlMap.commitTransaction();
           return ret;
       } catch (SQLException ex) {
           log.error(ex, ex);

       } finally {
           try {
               sqlMap.endTransaction();
           } catch (SQLException ex1) {
               log.warn(ex1, ex1);
           }
       }
       return null;

    }
    
    protected List consultaEnLista(String string, Object param) {
        try {
           sqlMap.startTransaction();
           List ret = sqlMap.queryForList(string, param);
           sqlMap.commitTransaction();
           return ret;
       } catch (SQLException ex) {
           log.error(ex, ex);

       } finally {
           try {
               sqlMap.endTransaction();
           } catch (SQLException ex1) {
               log.warn(ex1, ex1);
           }
       }
       return null;

    }
}
